package com.github.sbb.sherry.primitives.core;

import com.github.sbb.sherry.primitives.api.Facts;
import com.github.sbb.sherry.primitives.api.Rule;


public class BasicRule implements Rule {

  /**
   * Rule name.
   */
  protected String ruleName;

  /**
   * Rule priority.
   */
  protected int priority;

  public BasicRule(String ruleName) {
    this(ruleName, Rule.DEFAULT_PRIORITY);
  }

  public BasicRule(String ruleName, int priority) {
    this.ruleName = ruleName;
    this.priority = priority;
  }

  /**
   * 相同名称的规则被认为是同一个规则
   */
  @Override
  public String name() {
    return ruleName;
  }

  @Override
  public void init() {
    // no op
  }

  @Override
  public int getPriority() {
    return priority;
  }

  @Override
  public boolean evaluate(Facts facts) {
    return false;
  }

  @Override
  public void execute(Facts facts) throws Exception {
    // no op
  }

  @Override
  public int compareTo(Rule rule) {
    return Rule.super.compareTo(rule);
  }

  @Override
  public boolean equals(final Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    BasicRule basicRule = (BasicRule) o;
    return ruleName.equals(basicRule.ruleName);
  }

  @Override
  public int hashCode() {
    return ruleName.hashCode();
  }

  @Override
  public String toString() {
    return ruleName;
  }
}
